//Main.cpp
//Created 17/08/13
//Created By Daniel Bowler
//
//The main entry point in to our engine. Here we will create the main
//manager classes in memory (Application which will create the
//SceneManager and GraphicsManager (which will, in turn, create our ShaderManager)).
//We then init the graphics manager  (D3D resources).
//
//We then enter the game loop.
//
//NOTE: Debug build will a) show memory leaks (Application) and b) D3D unreleased
//resources (Essentially memory leakes caused by not releasing a reference to the
//underlying resource). Release builds do NOT provide these handy prompts, but
//our shaders WILL be compiled with code optimisation support - resulting in 
//faster shaders. 

#pragma once

//******************************************************************************
// Make sure you link: d3d11.lib d3dx11d.lib D3DCompiler.lib EffectsD.lib 
//                     dxerr.lib dxgi.lib dxguid.lib.
// Link d3dx11.lib and D3DX11Effects.lib for release mode builds instead
//   of d3dx11d.lib and D3DX11EffectsD.lib.
//Note, Effects11d and Effects11 is located in the root directory 
//of the application. 
//
//Note also: Remember to tell the linker where we have installed the DX SDK - all
//of my applications do this automatically but you could have some issues
//if you load the visual studio solution on your PC and install the DX SDK to a 
//differnt location that me.
//******************************************************************************
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "D3DCompiler.lib")
#pragma comment (lib, "dxerr.lib")
#pragma comment (lib, "dxgi.lib")

#if defined(DEBUG) | defined(_DEBUG)

#pragma comment (lib, "d3dx11d.lib")
#pragma comment (lib, "Effects11d.lib")

#else

#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "Effects11.lib")

#endif

//Required for Win32 applications - the basis required for a D3D application.
#include <Windows.h>
//Creates our manager classes, inits window and enters loop. Once the app
//closes (Ie, our loop is broken), it will also release
//all of our manager classes and resources they ahve allocated. 
#include "Application.h"
//Defines some useful debug includes - like the one that allows
//run time memory leak checks. 
#include "d3dUtil.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
				   PSTR cmdLine, int showCmd)
{
	// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

	//Seed random number generator.
	srand ((unsigned)time(NULL));

	//Create the application in memory - Note, our naming convention
	//for singletons is as follows: shared[ClassName].
	OutputDebugStringW(L"[Creating Application]\n");
	Application* sharedApplication = Application::GetInstance();
	
	//Init application - create window and allocate memory for the
	//singleton managers.
	sharedApplication->Init(hInstance);

	//Exit code - will be returned from WinMain. Init to 0 indicating no
	//issues.
	int exit = 0;
	//Enter Game Loop.
	exit = sharedApplication->Run();

	//Release application once done
	if (sharedApplication)
	{
		//Release - and set to null - no real need but its good
		//for muscle memory!
		sharedApplication->Release();
		sharedApplication = NULL;
		
		//Output message stating that everything has been released
		OutputDebugStringW(L"[Succesfully Released Manager Classes - Exiting]\n");
	}
	
	//Once done, return exit
	return exit;
}